package com.wys.singleton_demo.lazy;

import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author godShan
 * @Description:
 * @date 2019/5/5 12:16
 */
public class ContainerSingleton {

    private static Map<String, Object> ioc = new ConcurrentHashMap<>();

    public Object getBean(String className) {

        Object obj = null;

        if (StringUtils.isEmpty(className)) {
            return obj;
        }
        synchronized (ioc) {

            if (!ioc.containsKey(className)) {
                try {

                    Class clazz = Class.forName(className);
                    obj = clazz.newInstance();
                    ioc.put(className, obj);
                    return obj;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return ioc.get(className);
        }
    }

}
